package Stack;

import java.util.Arrays;
import java.util.Map;

public class MyStack {
    private int[] elem;
    private int usedSize;


    public MyStack() {
        this.elem = new int[5];
    }

    //在栈顶插入一个元素
    public void push(int val) {
        if(isFull()) {
            elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize++] = val;
    }

    //判断栈空间是否满
    public boolean isFull() {
        return usedSize == elem.length;
    }

    //删除栈顶元素
    public int pop() {
        if(isEmpty()) {
            throw new StackEmptyException("栈为空");
        }
        return elem[--usedSize];
        //usedSize--;
    }

    //判断是否为空
    public boolean isEmpty() {
        return usedSize == 0;
    }

    //获取栈顶元素
    public int peek() {
        if(isEmpty()) {
            throw new StackEmptyException("栈为空");
        }
        return elem[usedSize-1];
    }



}
